<?php
 session_start();
 
 
 
   //get email provided by user
 $email = $_POST['l_email'];
 $confirm_code=md5(uniqid(rand()));
 
 // validation check
 
 if ($email == "" )
{
	echo 'blank_email'; // for blank email address
	exit;
}
 else if (preg_match("/^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/", $email)) 
{
} 
else 
{
    echo 'invalid_email';
	exit;
}
 
 // connect to database
 include('config.php');
 
 
   $tbl_name1 = "user_info";
  $tbl_name2 = "temp_user";


// Create Mysqli object
$db = new mysqli($db_server,$db_user,$db_pass,$db_database);

// Create statement object
$stmt = $db->stmt_init();

// Create a prepared statement
if($stmt->prepare("SELECT email FROM $tbl_name1 WHERE email = ? ")) {
 
   
   // Bind your variable to replace the ?
    $stmt->bind_param('s', $email);
 
  
    // Execute query
    $stmt->execute();
	
	$email="";
	 // Bind your result columns to variables
    $stmt->bind_result($email);
	
	
	
	$stmt->fetch();

	
	// Close statement object
		$stmt->close();
	
	
	if($email!="")
	{
		// if email is not found in database
		//echo "no-email";
		
		

		// Create statement object
		$qry = $db->stmt_init();
		
		
	
	$xp_date= date('Y-m-d', strtotime('+2 days'));
	


		// Create a prepared statement
	if($qry->prepare("INSERT INTO $tbl_name2(email,confirm_code,expiry_date) VALUES ('$email','$confirm_code','$xp_date')"))
	 {
		
		
			 // Execute query
		$qry->execute();
		
		 // Close statement object
		$qry->close();
		
					// content of the email
					$message= "<html>";
					$message.= "<body>";
					$message.="<p>Your Password Reset link </p>";
					$message.="<p>Click on this link to reset your account password</p>";
					$message.="<p><a href='http://localhost/e-library/password-reset.php?a=$confirm_code' target='_blank'>Reset Password link</a></p>";
					
					$message .= "</html>";
					$message .= "</body>";
					
					// subject of the message
					$subject = "Password reset for Popular resumes account";
					
					include('mail-config.php');
					
						if(!$mail->Send())  // check if mail was sent or not
						 {
						  //echo "Mailer Error: " . $mail->ErrorInfo;
						  echo 'false';
						} 
						else {
						  echo 'true';
						}
			
		}
		else{
			echo "no sent";
			}
		
		}
	else
	{
		echo "no-email";
	}
	
	
	
	
		
	
}

 ?>
